{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tensorflow常见Layer层介绍"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tensorflow常见的Layer层介绍：\n",
    "\n",
    "1. tf.keras.layers.Dense\n",
    "2. tf.keras.layers.DenseFeatures\n",
    "3. tf.keras.layers.Flatten\n",
    "4. tf.keras.layers.ReLU\n",
    "5. tf.keras.layers.Multiply\n",
    "6. tf.keras.layers.Concatenate\n",
    "7. tf.keras.layers.Average\n",
    "8. tf.keras.layers.Dot\n",
    "9. tf.keras.layers.Lambda\n",
    "10. tf.keras.layers.Dropout\n",
    "11. tf.keras.layers.Embedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "tf.keras.backend.set_floatx('float64')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. tf.keras.layers.Dense\n",
    "\n",
    "即全连接层，实现了函数output = activation(dot(input, kernel) + bias)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 中间层：全连接层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:Layer dense is casting an input tensor from dtype float32 to the layer's dtype of float64, which is new behavior in TensorFlow 2.  The layer has dtype float64 because it's dtype defaults to floatx.\n",
      "\n",
      "If you intended to run this layer in float64, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.\n",
      "\n",
      "To change all layers to have dtype float32 by default, call `tf.keras.backend.set_floatx('float32')`. To change just this layer, pass dtype='float32' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(10, 5), dtype=float64, numpy=\n",
       "array([[0.        , 1.9249171 , 0.        , 0.89091609, 0.5583039 ],\n",
       "       [0.        , 1.81681698, 0.        , 0.65956321, 0.54372945],\n",
       "       [0.        , 1.54399701, 0.        , 1.3698133 , 0.96406378],\n",
       "       [0.        , 2.09359254, 0.        , 0.97597904, 0.00911146],\n",
       "       [0.        , 2.05280482, 0.        , 0.91712408, 1.17849202],\n",
       "       [0.        , 1.93756766, 0.        , 0.69925972, 0.53570477],\n",
       "       [0.        , 2.12235513, 0.        , 0.87475147, 0.61988459],\n",
       "       [0.        , 1.21363722, 0.        , 0.        , 0.43412224],\n",
       "       [0.        , 2.13034579, 0.        , 0.87204356, 1.0358732 ],\n",
       "       [0.        , 1.86375708, 0.        , 0.93378452, 0.05306395]])>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer = tf.keras.layers.Dense(5, activation='relu')\n",
    "inputs = tf.random.uniform(shape=(10, 20))\n",
    "layer(inputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 输出层：二分类sigmoid层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:Layer dense_1 is casting an input tensor from dtype float32 to the layer's dtype of float64, which is new behavior in TensorFlow 2.  The layer has dtype float64 because it's dtype defaults to floatx.\n",
      "\n",
      "If you intended to run this layer in float64, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.\n",
      "\n",
      "To change all layers to have dtype float32 by default, call `tf.keras.backend.set_floatx('float32')`. To change just this layer, pass dtype='float32' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(10, 1), dtype=float64, numpy=\n",
       "array([[0.57820843],\n",
       "       [0.5731226 ],\n",
       "       [0.52552678],\n",
       "       [0.64714436],\n",
       "       [0.58641704],\n",
       "       [0.88362153],\n",
       "       [0.74354683],\n",
       "       [0.64688738],\n",
       "       [0.56655084],\n",
       "       [0.59932964]])>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer = tf.keras.layers.Dense(1, activation='sigmoid')\n",
    "inputs = tf.random.uniform(shape=(10, 20))\n",
    "layer(inputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 输出层：多分类softmax层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:Layer dense_2 is casting an input tensor from dtype float32 to the layer's dtype of float64, which is new behavior in TensorFlow 2.  The layer has dtype float64 because it's dtype defaults to floatx.\n",
      "\n",
      "If you intended to run this layer in float64, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.\n",
      "\n",
      "To change all layers to have dtype float32 by default, call `tf.keras.backend.set_floatx('float32')`. To change just this layer, pass dtype='float32' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(10, 5), dtype=float64, numpy=\n",
       "array([[0.1401907 , 0.22347334, 0.38484299, 0.14510504, 0.10638793],\n",
       "       [0.14486136, 0.37215462, 0.21253336, 0.15036252, 0.12008814],\n",
       "       [0.1314145 , 0.14418986, 0.48736831, 0.16144951, 0.07557783],\n",
       "       [0.21411673, 0.2544252 , 0.18628143, 0.19428891, 0.15088773],\n",
       "       [0.37858763, 0.22696633, 0.17724576, 0.115779  , 0.10142129],\n",
       "       [0.18730001, 0.08372979, 0.38544624, 0.26785091, 0.07567306],\n",
       "       [0.16832084, 0.15507676, 0.34691556, 0.24755546, 0.08213139],\n",
       "       [0.12605279, 0.17856597, 0.43195407, 0.18286805, 0.08055912],\n",
       "       [0.36226068, 0.26535715, 0.16766208, 0.13236178, 0.07235832],\n",
       "       [0.09559492, 0.1674194 , 0.35323936, 0.26630703, 0.11743929]])>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer = tf.keras.layers.Dense(5, activation='softmax')\n",
    "inputs = tf.random.uniform(shape=(10, 20))\n",
    "layer(inputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. tf.keras.layers.DenseFeatures\n",
    "输入feature_columns，产出dense Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 1), dtype=float32, numpy=\n",
       "array([[-0.5 ],\n",
       "       [-0.25],\n",
       "       [ 0.  ],\n",
       "       [ 0.25],\n",
       "       [ 0.5 ]], dtype=float32)>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {\"age\" : [10, 20, 30, 40, 50]}\n",
    "feature_columns = [tf.feature_column.numeric_column(\"age\", normalizer_fn=lambda x : (x-30)/(50-10))]\n",
    "\n",
    "layer = tf.keras.layers.DenseFeatures(feature_columns)\n",
    "layer(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. tf.keras.layers.Flatten"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 2, 2)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.rand(5, 2, 2)\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 4), dtype=float64, numpy=\n",
       "array([[5.84191194e-01, 5.67937221e-01, 2.99804066e-01, 4.03932370e-01],\n",
       "       [2.50615772e-01, 7.93609844e-02, 8.00616697e-02, 3.20907690e-01],\n",
       "       [2.78647779e-01, 5.69644573e-01, 8.48539937e-02, 1.94493422e-01],\n",
       "       [9.67766976e-01, 2.63621147e-01, 4.43519858e-01, 6.65714531e-04],\n",
       "       [3.29980743e-01, 3.85580841e-01, 3.43525241e-01, 2.62776741e-01]])>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer = tf.keras.layers.Flatten()\n",
    "layer(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. tf.keras.layers.ReLU\n",
    "\n",
    "大于零的数字还是本身，小于零的全变成0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(7,), dtype=int32, numpy=array([0, 0, 0, 0, 1, 2, 3], dtype=int32)>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer = tf.keras.layers.ReLU()\n",
    "layer([-1, -2, -3, 0, 1, 2, 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. tf.keras.layers.Multiply\n",
    "\n",
    "相同形状的矩阵，对应元素相乘的乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 3), dtype=float64, numpy=\n",
       "array([[ 0.45275238, -0.13025436,  0.90234958],\n",
       "       [ 1.5401108 ,  0.08312545,  2.37077822],\n",
       "       [ 2.62746921,  0.29650526,  3.83920686],\n",
       "       [ 3.71482763,  0.50988508,  5.3076355 ],\n",
       "       [ 4.80218605,  0.72326489,  6.77606414]])>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(10).reshape(5, 2)\n",
    "x1 = tf.keras.layers.Dense(3)(data)\n",
    "x1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 3), dtype=float64, numpy=\n",
       "array([[ -9.78785725,  10.90648852,  10.58490297],\n",
       "       [-11.5401339 ,  12.95956377,  12.54337819],\n",
       "       [-13.29241055,  15.01263903,  14.50185341],\n",
       "       [-15.0446872 ,  17.06571428,  16.46032862],\n",
       "       [-16.79696385,  19.11878953,  18.41880384]])>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(10, 20).reshape(5, 2)\n",
    "x2 = tf.keras.layers.Dense(3)(data)\n",
    "x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TensorShape([5, 3])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "multiplied = tf.keras.layers.Multiply()([x1, x2])\n",
    "multiplied.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. tf.keras.layers.Concatenate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 2), dtype=float64, numpy=\n",
       "array([[-1.21766837,  0.14929667],\n",
       "       [-2.03101023, -1.53975052],\n",
       "       [-2.84435209, -3.22879772],\n",
       "       [-3.65769396, -4.91784492],\n",
       "       [-4.47103582, -6.60689211]])>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1 = tf.keras.layers.Dense(2)(np.arange(10).reshape(5, 2))\n",
    "x1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 2), dtype=float64, numpy=\n",
       "array([[ 3.17020045,  8.64574809],\n",
       "       [ 3.6010728 , 10.23822072],\n",
       "       [ 4.03194514, 11.83069335],\n",
       "       [ 4.46281748, 13.42316598],\n",
       "       [ 4.89368982, 15.01563861]])>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2 = tf.keras.layers.Dense(2)(np.arange(10, 20).reshape(5, 2))\n",
    "x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 4), dtype=float64, numpy=\n",
       "array([[-1.21766837,  0.14929667,  3.17020045,  8.64574809],\n",
       "       [-2.03101023, -1.53975052,  3.6010728 , 10.23822072],\n",
       "       [-2.84435209, -3.22879772,  4.03194514, 11.83069335],\n",
       "       [-3.65769396, -4.91784492,  4.46281748, 13.42316598],\n",
       "       [-4.47103582, -6.60689211,  4.89368982, 15.01563861]])>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "concatted = tf.keras.layers.Concatenate()([x1, x2])\n",
    "concatted"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7. tf.keras.layers.Average"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还有类似的：\n",
    "* tf.keras.layers.Minimum\n",
    "* tf.keras.layers.Maximum\n",
    "* tf.keras.layers.Subtract\n",
    "* tf.keras.layers.Add"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = np.ones((2, 2))\n",
    "x2 = np.zeros((2, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(2, 2), dtype=float64, numpy=\n",
       "array([[0.5, 0.5],\n",
       "       [0.5, 0.5]])>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.keras.layers.Average()([x1, x2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(2, 2), dtype=float64, numpy=\n",
       "array([[1., 1.],\n",
       "       [1., 1.]])>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.keras.layers.Add()([x1, x2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8. tf.keras.layers.Dot\n",
    "\n",
    "两个形状是：(batch_size, n)，点积结果是(batch_size, 1)，其中第i个元素是a[i]和b[i]的点积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 3), dtype=float64, numpy=\n",
       "array([[ 0.14792895, -0.32379876, -0.93698386],\n",
       "       [-0.59872396, -2.50572416, -1.39362195],\n",
       "       [-1.34537686, -4.68764956, -1.85026004],\n",
       "       [-2.09202976, -6.86957496, -2.30689813],\n",
       "       [-2.83868266, -9.05150036, -2.76353622]])>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1 = tf.keras.layers.Dense(3)(np.arange(10).reshape(5, 2))\n",
    "x1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 3), dtype=float64, numpy=\n",
       "array([[ 5.27100268,  4.31044328, 20.50256482],\n",
       "       [ 6.12875283,  5.24824751, 24.38756573],\n",
       "       [ 6.98650298,  6.18605174, 28.27256663],\n",
       "       [ 7.84425313,  7.12385597, 32.15756754],\n",
       "       [ 8.70200328,  8.0616602 , 36.04256844]])>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2 = tf.keras.layers.Dense(3)(np.arange(10, 20).reshape(5, 2))\n",
    "x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 1), dtype=float64, numpy=\n",
       "array([[ -19.82655461],\n",
       "       [ -50.80713859],\n",
       "       [ -90.70912238],\n",
       "       [-139.53250597],\n",
       "       [-197.27728937]])>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dotted = tf.keras.layers.Dot(axes=1)([x1, x2])\n",
    "dotted"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 9. tf.keras.layers.Lambda"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将任意表达式函数，封装成Layer对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(12).reshape(3,4)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(3, 4), dtype=int64, numpy=\n",
       "array([[ 0,  2,  4,  6],\n",
       "       [ 8, 10, 12, 14],\n",
       "       [16, 18, 20, 22]])>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer = tf.keras.layers.Lambda(lambda x: x * 2)\n",
    "\n",
    "layer(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10. tf.keras.layers.Dropout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 1.],\n",
       "       [2., 3.],\n",
       "       [4., 5.],\n",
       "       [6., 7.],\n",
       "       [8., 9.]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(10).reshape(5, 2).astype(np.float64)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用随机的概率将输入数据设置成0，降低过拟合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5, 2), dtype=float64, numpy=\n",
       "array([[ 0.        ,  1.42857143],\n",
       "       [ 2.85714286,  4.28571429],\n",
       "       [ 5.71428571,  0.        ],\n",
       "       [ 8.57142857, 10.        ],\n",
       "       [11.42857143,  0.        ]])>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer = tf.keras.layers.Dropout(rate=0.3)\n",
    "layer(data, training=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 11. tf.keras.layers.Embedding"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "解释：\n",
    "* 输入一个大小为8的一维数组，每个都是ID数字；\n",
    "* 目的是把每个数字，都表示成一个embedding向量，向量的dim是可调整的参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([38, 65, 73, 41, 76, 33, 64, 26])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 原始数据，正式ID列表，注意100是这里的最大值\n",
    "data = np.random.randint(100, size=(8, ))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构造Embedding层，其中第一个参数100是ID词汇表的大小，第二个5是每个embedding的维度，\n",
    "layer = tf.keras.layers.Embedding(100, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(8, 5), dtype=float64, numpy=\n",
       "array([[ 0.03641005, -0.03096094, -0.02933977, -0.02728719,  0.03820207],\n",
       "       [ 0.01415709, -0.03936355, -0.03412845,  0.01089082, -0.03490474],\n",
       "       [-0.04527347, -0.0179525 , -0.01400301, -0.00982093, -0.02419136],\n",
       "       [ 0.03526746,  0.00917123, -0.00622018,  0.0064143 , -0.00599041],\n",
       "       [-0.0475856 ,  0.03995157,  0.01552021,  0.04905842,  0.04825909],\n",
       "       [-0.04175891,  0.00880192,  0.03129269, -0.03555738, -0.03965375],\n",
       "       [-0.00184724,  0.02565859, -0.03047455,  0.01181022,  0.03072589],\n",
       "       [ 0.02453402, -0.00904046, -0.01955357, -0.00082403,  0.02327346]])>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出为(8, 5)，意思是给每个ID，都产出了一个5维度的Embedding向量"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
